package cn.wolfcode.edu.service.impl;

import cn.wolfcode.edu.anno.PermissionName;
import cn.wolfcode.edu.domain.Permission;
import cn.wolfcode.edu.mapper.PermissionMapper;
import cn.wolfcode.edu.query.QueryObject;
import cn.wolfcode.edu.service.IPermissionService;
import cn.wolfcode.edu.util.PageResult;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * Created by abb on 2018-07-09.
 */
@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private ApplicationContext applicationContext;


    @Override
    public void deleteByPrimaryKey(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insert(Permission entity) {
        permissionMapper.insert(entity);
    }

    @Override
    public Permission selectByPrimaryKey(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> selectAll() {
        return permissionMapper.selectAll();
    }

    @Override
    public void updateByPrimaryKey(Permission entity) {
        permissionMapper.updateByPrimaryKey(entity);
    }

    @Override
    public PageResult query(QueryObject qo) {
        int total = permissionMapper.queryForCount(qo);
        List<Permission> rows = permissionMapper.queryForList(qo);
        return new PageResult(total, rows);
    }

    public PageResult listAll() {
        List<Permission> list = permissionMapper.selectAll();
        PageResult pageResult = new PageResult(list.size(), list);
        return pageResult;
    }

    public PageResult queryByRoleId(Long roleId) {
        List<Permission> list = permissionMapper.queryByRoleId(roleId);
        return new PageResult(list.size(), list);
    }

    public void reload() {
        List<String> permissionsNames = permissionMapper.selectAllName();
        RequestMappingHandlerMapping bean = applicationContext.getBean(RequestMappingHandlerMapping.class);
        Map<RequestMappingInfo, HandlerMethod> handlerMethods = bean.getHandlerMethods();
        Collection<HandlerMethod> methods = handlerMethods.values();
        for (HandlerMethod met : methods) {
            PermissionName permissionName = met.getMethodAnnotation(PermissionName.class);
            if (permissionName != null) {


                String name = permissionName.value();


                if (!permissionsNames.contains(name)) {


                    RequiresPermissions requiresPermissions = met.getMethodAnnotation(RequiresPermissions.class);

                    String[] resources = requiresPermissions.value();


                    String resource = StringUtils.join(resources, ",");


                    Permission permission = new Permission();
                    permission.setName(name);
                    permission.setResource(resource);
                    permissionMapper.insert(permission);
                }
            }

        }


    }
}
